# Maintainer: Celeste <cielesti@protonmail.com>
maintainer="Celeste <cielesti@protonmail.com>"
pkgname=mongooseim
pkgver=6.2.1
pkgrel=4
pkgdesc="Robust, scalable and efficient XMPP server aimed at large installations"
url="https://www.erlang-solutions.com/products/mongooseim.html"
# loongarch64: rebar3 fails to fetch/verify dependencies
arch="all !loongarch64"
license="GPL-2.0-or-later"
depends="bash erlang runuser"
makedepends="
	erlang-dev
	openssl-dev
	re2-dev
	rebar3
	unixodbc-dev
	zlib-dev
	"
# !check: tests require a running server
# !fhs: /var/lock/mongooseim/ctl is used by mongooseimctl
options="!check !fhs"
install="$pkgname.pre-install"
pkgusers="mongooseim"
pkggroups="mongooseim"
subpackages="$pkgname-dev $pkgname-openrc"
source="https://github.com/esl/MongooseIM/archive/$pkgver/mongooseim-$pkgver.tar.gz
	$pkgname.initd

	busybox-flock.patch
	disable-system-metrics.patch
	dont-install-self-signed-cert.patch
	fix-vcard-domain-warning.patch
	use-https-for-github.patch
	"
builddir="$srcdir/MongooseIM-$pkgver"

export SKIP_CERT_BUILD=1

prepare() {
	default_prepare

	rebar3 as prod get-deps
}

build() {
	tools/configure with-all without-jingle-sip \
		prefix="" system=yes user="mongooseim"

	make REBAR=/usr/bin/rebar3
}

package() {
	tools/configure with-all without-jingle-sip \
		prefix="$pkgdir" system=yes user="mongooseim"
	(
		# shellcheck disable=1091
		. ./configure.out && tools/install
	)

	# Remove unnecessary source files
	rm -rf "$pkgdir"/usr/lib/mongooseim/lib/*/src \
		"$pkgdir"/usr/lib/mongooseim/lib/*/c_src

	# Remove duplicate libs already in erlang
	local dup; for dup in $(find /usr/lib/erlang/lib \
		"$pkgdir"/usr/lib/mongooseim/lib \
		-mindepth 1 -maxdepth 1 -type d -print0 \
		| xargs -0 -n1 basename \
		| sort | uniq -d)
	do
		rm -rf "$pkgdir"/usr/lib/mongooseim/lib/"$dup"
		ln -s /usr/lib/erlang/lib/"$dup" \
			"$pkgdir"/usr/lib/mongooseim/lib/
	done

	# Fix permissions
	chown -R 0:0 "$pkgdir"/usr/lib/mongooseim \
		"$pkgdir"/usr/bin/mongooseimctl
	mkdir "$pkgdir"/usr/lib/mongooseim/log
	chown -R mongooseim:mongooseim \
		"$pkgdir"/usr/lib/mongooseim/log \
		"$pkgdir"/usr/lib/mongooseim/priv

	install -Dm755 "$srcdir"/$pkgname.initd \
		"$pkgdir"/etc/init.d/$pkgname
}

sha512sums="
ee36ced041aed60bbf7c34ee6dcfcc838337b57ddbafaacb12045b460933cea18a04cb7b9b1e8748a517345f8cb99b746232eb5d13131b1e859cc64f9a545257  mongooseim-6.2.1.tar.gz
446ec26377214e028ee4608e296de929e5207a20af73e143b9beb0af137a9d928731a1c4e2e5518a54cb311dbb46143d9cdfcd01ec698ebdfc1bfb8b2848f314  mongooseim.initd
3ad4758baf700ff55574b05f49be4eda6969bccf7dcfc3615c896b917b88930485bd0e5f75840b312a5eb50a655911cec788a39cc56511ae12af2c174c48318c  busybox-flock.patch
c14a47bb37e782848cdbeda094e5d417cf2d4987eafc2f60ff763367ede4422ff1e683386131601a793541f763caae09430c9539afdc1c9f4811dd997da7f5f4  disable-system-metrics.patch
4e5b16220bbf86c2907d159aa4b021d3294f2f625d3f0175ac418ccdff56a05a6a75cc11308e7387baaf0eaed5a3dcffebc39f8e74301234f189439041eefff6  dont-install-self-signed-cert.patch
54270ece5503ff103d0757b3d19009beb36671586036e92992c88bafaa2fa6a4bdfc700c7fa07c240f8b64da2e6add2815ee5b2f9c46d6f530943c86dbece4f4  fix-vcard-domain-warning.patch
2147d8d3c9bec6f1896903ade16fdf004597f665395a3401c6e07304f695cab279c2006d91f820f5f4a4dd0afd9a484bdc600216fd63aa54a62e08ae47abca29  use-https-for-github.patch
"
